package com.example.seakdreamandroid.sleepRecord;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.sleep.entity.SleepInfo;

import java.util.List;

public class SleepRecordAdapter extends RecyclerView.Adapter{
    private List<SleepInfo> records;
    private Context context;
    private static final int LAYOUT_ID = R.layout.item_sleep_record;

    public SleepRecordAdapter(List<SleepInfo> records, Context context) {
        this.records = records;
        this.context = context;
    }

    public void setData(List<SleepInfo> records){
        this.records = records;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(LAYOUT_ID,parent,false);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SleepInfo record = records.get(position);
        MyHolder myHolder = (MyHolder) holder;
        long minutes = record.getDuration();
        long hour = minutes/60;
        long minute = minutes%60;
        Log.i(SleepRecordAdapter.class.getName(),"minutes = " + minutes + ",minute" + minute);
        myHolder.durationHour.setText(hour+"");
        myHolder.durationMinute.setText(minute+"");
        myHolder.date.setText(record.getFormatDate());
        myHolder.dayOfWeek.setText(record.getDayOfWeek());
        myHolder.startTime.setText(record.getFormatStartTime());
        myHolder.endTime.setText(record.getFormatEndTime());
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    private class MyHolder extends RecyclerView.ViewHolder{
        private TextView durationHour;
        private TextView durationMinute;
        private TextView date;
        private TextView dayOfWeek;
        private TextView startTime;
        private TextView endTime;
        private View v;
        public MyHolder(@NonNull View v) {
            super(v);
            this.v = v;
            durationHour = v.findViewById(R.id.duration_hour);
            durationMinute = v.findViewById(R.id.duration_minute);
            date = v.findViewById(R.id.date);
            dayOfWeek = v.findViewById(R.id.day_of_week);
            startTime = v.findViewById(R.id.start_time);
            endTime = v.findViewById(R.id.end_time);
        }
    }
}
